﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InsuranceSell.Code.Entities;
using System.Data.SqlClient;
using System.Data;

namespace InsuranceSell.Code.Data
{
    class UserDAO : BaseDAO
    {
        public bool AddNewUser(User user)
        {
            if (FindUserName(user.Username))
            {
                using (SqlConnection connection = GetConnection())
                {
                    SqlCommand command = new SqlCommand("sp_AddNewUser", connection);
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add(new SqlParameter("@FullName", user.FullName));
                    command.Parameters.Add(new SqlParameter("@Username", user.Username));
                    command.Parameters.Add(new SqlParameter("@Password", user.Password));
                    command.Parameters.Add(new SqlParameter("@Email", user.Email));
                    command.Parameters.Add(new SqlParameter("@Mobile", user.Mobile));
                    command.Parameters.Add(new SqlParameter("@Telephone", user.Telephone));
                    command.Parameters.Add(new SqlParameter("@isActive", user.isActive));
                    command.Parameters.Add(new SqlParameter("@isAdmin", user.isAdmin));
                    try
                    {
                        connection.Open();
                        return (command.ExecuteNonQuery() != 0);
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }
                }
            }
            else { return false; }
        }

        public DataTable LoadAllUsers()
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("sp_LoadAllUser", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);
                DataTable allUsers = new DataTable();

                try
                {
                    connection.Open();
                    sqlAdapter.Fill(allUsers);
                    return allUsers;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }

        public bool DeleteUser(User user)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("sp_DeleteUser", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@Username", user.Username));
                try
                {
                    connection.Open();
                    return (command.ExecuteNonQuery() != 0);
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public bool FindUserName(string username)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("sp_FindUserName", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@Username", username));
                SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);
                DataTable aUser = new DataTable();

                try
                {
                    connection.Open();
                    sqlAdapter.Fill(aUser);
                    if (aUser.Rows.Count != 0)
                    {
                        return false;
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public bool UpdateUser(User user)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command;
                if (user.Password == Encryptor.HashMD5(""))
                {
                    command = new SqlCommand("sp_UpdateUserWithourPass", connection);
                }
                else
                {
                    command = new SqlCommand("sp_UpdateUser", connection);
                }
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@FullName", user.FullName));
                command.Parameters.Add(new SqlParameter("@Username", user.Username));
                command.Parameters.Add(new SqlParameter("@Password", user.Password));
                command.Parameters.Add(new SqlParameter("@Email", user.Email));
                command.Parameters.Add(new SqlParameter("@Mobile", user.Mobile));
                command.Parameters.Add(new SqlParameter("@Telephone", user.Telephone));
                command.Parameters.Add(new SqlParameter("@isActive", user.isActive));
                command.Parameters.Add(new SqlParameter("@isAdmin", user.isAdmin));
                try
                {
                    connection.Open();
                    return (command.ExecuteNonQuery() != 0);
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
        public DataTable CheckUserLogin(string username, string password)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("sp_CheckUserLogin", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@Username", username));
                command.Parameters.Add(new SqlParameter("@Password", password));

                SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);
                DataTable userInfo = new DataTable();

                try
                {
                    connection.Open();
                    sqlAdapter.Fill(userInfo);
                }
                catch (Exception ex)
                {
                    return null;
                }
                return userInfo;
            }
        }
    }
}
